/* @(#)GSOrderStatusUpateAfterPrintAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-4-12     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderDetailBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSOrderSubmitForNonFlatForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSOrderStatusUpateAfterPrintAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        GSOrderSubmitForNonFlatForm tForm = (GSOrderSubmitForNonFlatForm)aForm;
        
        //取得订单明细编号
        ArrayList tOrderDetailId = new ArrayList();
        if(tForm.orderDtlTempIds != null && tForm.orderDtlTempIds.length > 0){
            for(int i=0; i<tForm.orderDtlTempIds.length; i++){
                if(tForm.orderDtlTempIds[i]  != null){
                    tOrderDetailId.add(tForm.orderDtlTempIds[i]);
                }
            }
        }else{
            tForm.getRequest().setAttribute("action","A150401.do");
            GSLogWriter.error(this,GSException.E00003_MSG);
            throw new GSException(GSException.E00003,GSException.E00003_MSG);
        }
        //取得当前操作员信息
        GSOperatorBean tOperBean = (GSOperatorBean)tForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        Connection tConn = null;
        GSOrderDetailDAO tOrderDtlDAO = new GSOrderDetailDAO();
        try{
            boolean tFlag = false;
            
            tConn = tOrderDtlDAO.getConnetion();
            for(int i=0; i< tOrderDetailId.size(); i++){
                GSOrderDetailBean tOrderDtlBean = new GSOrderDetailBean();
                tOrderDtlBean.setGs_order_detail_id((String)tOrderDetailId.get(i));
                tOrderDtlBean.setGs_deliver_goods_id(tForm.deliverGoodsId);
                tOrderDtlBean.setGs_lastmodifer_id(tOperBean.getGs_oper_id());
                tOrderDtlBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDERING);
                tOrderDtlBean.setGs_lastmodify_date(DateUtil.formateDateTime(DateUtil.getCurrentTime("DT","AD")));
                tOrderDtlBean.setGs_lastmodify_contents("修改订单明细状态为订货中");
                //修改订单明细状态为订货中
                tFlag =  tOrderDtlDAO.updateOrderDetailStatus(tOrderDtlBean,tConn,GSBaseConstants.ORDER_AUTHORISE);
                if(!tFlag){
                    tConn.rollback();
                    GSLogWriter.error(this,"网下订单明细打印后修改订单状态为订货中失败!");
                    tForm.getRequest().setAttribute("message","网下订单明细打印后修改订单状态为订货中失败!");
                    break;
                }
            }
            if(tFlag){
                tConn.commit();
                GSLogWriter.info(this,"网下订单明细打印后修改订单状态为订货中成功!");
                tForm.getRequest().setAttribute("message","网下订单明细打印后修改订单状态为订货中成功!");
            }
            
            //取得当前操作员可查询的订货单位和送货地址
            GSOrderDeptInfoProcessor tProc = new GSOrderDeptInfoProcessor();
            HashMap tMap = tProc.getOrderDeptInfoOfBusOper(tOperBean.getGs_oper_org_id());
            tForm.getRequest().setAttribute("orderdept",tMap);
            //取得产品分类
            GSManufactureProcessor tManufProc = new GSManufactureProcessor();
            tManufProc.getManufactureOfLevel(tForm);
            tForm.getRequest().setAttribute("requery","true");
            
        }catch(GSException ge){
            try{
                if(tConn != null)
                    tConn.rollback();
            }catch(SQLException se){
                se.printStackTrace();
            }
            throw ge;
        }catch(Exception e){
            try{
                if(tConn != null)
                    tConn.rollback();
            }catch(SQLException se){
                se.printStackTrace();
            }
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E10051,GSException.E10051_MSG);
        }finally{
            tConn.close();
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
